#!/usr/bin/env python
# -*- coding:utf-8 -*-

# https://www.cnblogs.com/ruo-li-suo-yi/p/7425307.html
# Python Tkinter Grid布局管理器详解
# grid 表格布局，采用表格结构组织组件。
# 子组件的位置由行和列的单元格来确定，并且可以跨行和跨列，从而实现复杂的布局。
# 注意：不要试图在一个主窗口中混合使用pack和grid

import tkinter
from public_function import dict_choice


def test_1():
    tk = tkinter.Tk()
    tkinter.Label(tk, text="First").grid(row=0)  # 标签控件，显示文本和位图，展示在第一行
    tkinter.Label(tk, text="Second").grid(row=1)  # 第二行

    tkinter.mainloop()


def test_2():
    tk = tkinter.Tk()
    tkinter.Label(tk, text="First").grid(row=0)  # 标签控件，显示文本和位图，展示在第一行
    tkinter.Label(tk, text="Second").grid(row=1)  # 第二行

    # 输入控件
    tkinter.Entry(tk).grid(row=0, column=1)
    tkinter.Entry(tk).grid(row=1, column=1)

    tkinter.mainloop()


# sticky参数的使用
def test_3():
    tk = tkinter.Tk()
    tkinter.Label(tk, text="First").grid(row=0, sticky=tkinter.E)  # 靠右
    tkinter.Label(tk, text="Second").grid(row=2, sticky=tkinter.W)  # 第二行，靠左

    # 输入控件
    tkinter.Entry(tk).grid(row=0, column=1)
    tkinter.Entry(tk).grid(row=2, column=1)

    tkinter.mainloop()


# columnspan选项可以指定控件跨越多列显示，而rowspan选项同样可以指定控件跨越多行显示。
def test_4():
    tk = tkinter.Tk()
    var = tkinter.IntVar()

    # 标签控件，显示文本和位图，展示在第一行
    tkinter.Label(tk, text="First").grid(row=0, sticky=tkinter.E)  # 靠右
    tkinter.Label(tk, text="Second").grid(row=1, sticky=tkinter.W)  # 第二行，靠左

    # 输入控件
    tkinter.Entry(tk).grid(row=0, column=1)
    tkinter.Entry(tk).grid(row=1, column=1)

    button = tkinter.Checkbutton(tk, text="Precerve aspect", variable=var)
    # button.grid(sticky=W)
    button.grid(sticky=tkinter.W, columnspan=2)

    tkinter.mainloop()


def test_5():
    tk = tkinter.Tk()
    var = tkinter.IntVar()

    # 标签控件，显示文本和位图，展示在第一行
    tkinter.Label(tk, text="First").grid(row=0, sticky=tkinter.E)  # 靠右
    tkinter.Label(tk, text="Second").grid(row=1, sticky=tkinter.W)  # 第二行，靠左

    # 输入控件
    tkinter.Entry(tk).grid(row=0, column=1, padx=10, pady=10)
    tkinter.Entry(tk).grid(row=1, column=1)

    # 多选框插件
    button = tkinter.Checkbutton(tk, text="Precerve aspect", variable=var)
    button.grid(row=2, columnspan=2, sticky=tkinter.W)

    # 插入图片
    photo = tkinter.PhotoImage(file="../res/python.gif")
    label = tkinter.Label(image=photo)
    label.image = photo
    label.grid(row=0, column=2, rowspan=2, columnspan=2, padx=5, pady=5,
               sticky=tkinter.W + tkinter.E + tkinter.N + tkinter.S)  # 合并两行，两列，居中，四周外延5个长度

    # 按钮控件
    button1 = tkinter.Button(tk, text="Zoom in")
    button1.grid(row=2, column=2)
    button2 = tkinter.Button(tk, text="Zoom out")
    button2.grid(row=2, column=3)

    tkinter.mainloop()


# 利用padx和pady，可以将框架边界区分开
def test_6():
    t = tkinter.Tk()
    t.title('与python聊天中')

    # 创建frame容器
    frmLT = tkinter.Frame(width=500, height=320, bg='white')
    frmLC = tkinter.Frame(width=500, height=150, bg='red')
    frmLB = tkinter.Frame(width=500, height=30)
    frmRT = tkinter.Frame(width=200, height=500)

    frmLT.grid(row=0, column=0, padx=1, pady=3)
    frmLC.grid(row=1, column=0, padx=1, pady=3)
    frmLB.grid(row=2, column=0)
    frmRT.grid(row=0, column=1, rowspan=3, padx=2, pady=3)

    tkinter.mainloop()


def test_7():
    t = tkinter.Tk()
    t.title('与python聊天中')

    # 创建frame容器
    frmLT = tkinter.Frame(width=500, height=320, bg='white')
    frmLC = tkinter.Frame(width=500, height=150, bg='red')
    frmLB = tkinter.Frame(width=500, height=30)
    frmRT = tkinter.Frame(width=200, height=500)

    frmLT.grid(row=0, column=0, padx=1, pady=3)
    frmLC.grid(row=1, column=0, padx=1, pady=3)
    frmLB.grid(row=2, column=0)
    frmRT.grid(row=0, column=1, rowspan=3, padx=2, pady=3)

    # 添加按钮
    btnSend = tkinter.Button(frmLB, text='发 送', width=8)  # 在frmLB容器中添加
    btnSend.grid(row=2, column=0)
    btnCancel = tkinter.Button(frmLB, text='取消', width=8)
    btnCancel.grid(row=2, column=1, sticky=tkinter.E)

    # 添加图片
    imgInfo = tkinter.PhotoImage(file="python.gif")
    lblImage = tkinter.Label(frmRT, image=imgInfo)
    lblImage.image = imgInfo
    lblImage.grid()

    # 固定容器大小
    frmLT.grid_propagate(0)
    frmLC.grid_propagate(0)
    frmLB.grid_propagate(0)
    frmRT.grid_propagate(0)

    tkinter.mainloop()


def make_choice(dict_choice):
    choice = str(input("Enter a positive integer to choice: "))
    if choice in dict_choice.keys():
        exec(dict_choice[choice])
    else:
        print("输入有误，请重输！", choice)


def main():
    make_choice(dict_choice)


if __name__ == '__main__':
    main()
